﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Report
{
    public partial class frmckdtab : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmckdtab()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format("select * from 出库单 where 日期>='{0}' and 日期<='{1}' order by 日期,单据号", dateTimePicker1.Text,dateTimePicker2.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
        }
        public void jz()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("select * from 出库单 where 日期>='{0}' and 日期<='{1}' order by 日期,单据号", dateTimePicker1.Text, dateTimePicker2.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
        }

        private void 红冲ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count == 0)//判断是否选中某行
            {
                MessageBox.Show("请选中单据");
            }
            else
            {
                //执行操作
                //出库单表插入一条红冲单据
                insethc();
                //删除收入明细表
                scsr();
               
                //修改库存数量
                upkucun();
                //删除出库明细表
                scckmx();
                MessageBox.Show("红冲完成");
                jz();
            }
        }
        public void upkucun()
        {
            //根据单据号查询出库的商品
            SqlConnection conn = new SqlConnection(constr);
            SqlConnection conn1 = new SqlConnection(constr);
            SqlConnection conn2 = new SqlConnection(constr);
            conn2.Open();
            conn1.Open();
            conn.Open();
            decimal dqkc;
            decimal upkc;
            string sql = string.Format("SELECT [商品编号],[商品名称] ,[出库数量] FROM [其它出库明细] where 单据号='{0}'",dataGridView1.SelectedCells[1].Value.ToString());
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            sda.Fill(dt);
            //遍历dt表
            for (int i =0; i < dt.Rows.Count; i++)
            {
                //查寻当前库存
                string sql1 = string.Format("SELECT [iamount] FROM [items] where iid='{0}' and iname='{1}'", dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString());
                SqlCommand comm1 = new SqlCommand(sql1,conn1);
                SqlDataReader sdr = comm1.ExecuteReader();
                while (sdr.Read())
                {
                    dqkc =decimal.Parse( sdr["iamount"].ToString());
                    upkc = dqkc + decimal.Parse(dt.Rows[i][2].ToString());
                    //修改库存
                    string sqlup = string.Format("UPDATE [items] SET [iamount] = '{0}' WHERE iid='{1}' and iname='{2}'",upkc, dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString());
                    SqlCommand comm2 = new SqlCommand(sqlup,conn2);
                    comm2.ExecuteNonQuery();
                }
                sdr.Close();
            }
            conn.Close();
            conn1.Close();
            conn2.Close();
        }
        public void scckmx()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format( "DELETE FROM [其它出库明细] WHERE 单据号='{0}'",dataGridView1.SelectedCells[1].Value.ToString());
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();
            conn.Close();
        }
        public void scsr()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format( "DELETE FROM [收入明细] WHERE djh='{0}'", dataGridView1.SelectedCells[1].Value.ToString());
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();
            conn.Close();
        }
        public void insethc()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format( "INSERT INTO [出库单] ([单据号] ,[日期] ,[往来单位] ,[经手人]  ,[单据金额]  ,[收款金额] ,[优惠金额] ,[单据备注]   ,[hc]) VALUES ('{0}' ,'{1}' ,'{2}' ,'{3}' ,'{4}'  ,'{5}'  ,'{6}' ,'{7}'  ,1)",dataGridView1.SelectedCells[1].Value.ToString(), dataGridView1.SelectedCells[2].Value.ToString(), dataGridView1.SelectedCells[3].Value.ToString(), dataGridView1.SelectedCells[4].Value.ToString(),dataGridView1.SelectedCells[5].Value.ToString(), dataGridView1.SelectedCells[6].Value.ToString(), dataGridView1.SelectedCells[7].Value.ToString(), dataGridView1.SelectedCells[8].Value.ToString());
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();
            conn.Close();
            //MessageBox.Show("ok");
        }

        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
           
            if (e.RowIndex > dataGridView1.Rows.Count)
            {
                return;
            }
            else
            {
                try
                {
                    DataGridViewRow dgrSingle = dataGridView1.Rows[e.RowIndex];
                    if (Int32.Parse(dgrSingle.Cells[9].Value.ToString()) == 1)
                    {
                        dgrSingle.DefaultCellStyle.ForeColor = Color.Red;
                    }
                }
                catch { }
            }
            
        }
    }
}
